package com.llxz.blog.ucenter.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.llxz.blog.ucenter.client.DeleteArticleById;
import com.llxz.blog.ucenter.entity.User;
import com.llxz.blog.ucenter.service.UserService;
import com.llxz.blog.util.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author lyq
 * @description: 管理员控制器
 * date: 2021/9/4 15:09
 */

@Api(tags = "后台用户")
@RestController
@RequestMapping("/ucenter/admin")
public class AdminController {
    @Autowired
    UserService userService;

    @Autowired
    DeleteArticleById deleteArticleById;

    //分页查询
    @GetMapping("page/{current}/{limit}")
    public R page(@PathVariable Long current, @PathVariable Long limit){
        Page<User> page = new Page<>(current,limit);
        IPage<User> pageModel= userService.page(page);
        return R.ok().data("items",pageModel.getRecords()).data("total",pageModel.getTotal());

    }
    //删除
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id){
        User tmp = userService.getById(id);
        //删除这个人所有的文章
        deleteArticleById.deleteOnesArticle(tmp.getMobile());
        System.out.println("........................................................");
//        删除这个人的信息--状态改为false
        userService.removeById(id);
        return R.ok().message("删除成功");
    }
    //禁用
    @PutMapping("disable/{id}")
    public R disable(@PathVariable String id){
        User user = new User();
        user.setId(id);
        user.setIsDisable(true);
        userService.updateById(user);
        return R.ok().message("禁用成功");
    }

    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day) {
        Integer count = userService.countRegisterDay(day);
        return R.ok().data("countRegister",count);
    }
}